//get的基础自定义
import { HOST } from './HOST'
import { Indicator } from 'mint-ui'; //加载动效
import { Toast } from 'mint-ui'; //提示弹窗
const loadingdelay = 2500
    // import { MessageBox } from 'mint-ui'; //信息弹窗

//import vue from 'vue'

function err_dispose(err) {
    //异常处理函数
    //console.info('错误', err)
    if (err.status == 500) {
        Toast({
            message: '请求失败请检查网络',
            position: 'top',
            duration: 2000
        })
    } else {
        Toast({
            message: '请求失败没返回',
            position: 'top',
            duration: 2000
        })
    }
}

function res_dispose(res, resolve, reject) {
    //自定义业务返回信息判断
    let data = res.data
        // if (data.code == -1) {
        //     Toast({
        //         message: data.message,
        //         position: 'top',
        //         duration: 2000
        //     })
        // }


}


import qs from 'qs'; //请求参数处理库
import axios from 'axios' //请求库
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //请求头
axios.defaults.baseURL = HOST; //url公共前缀


export default {
    get: function(url, info) {
        //get请求    
        return new Promise(function(resolve, reject) {
            let delay = setTimeout(
                () => {
                    Indicator.open({
                        text: '加载中...',
                        spinnerType: 'fading-circle'
                    })
                },
                loadingdelay

            )

            //get请求
            axios.get(url, { params: info }).then(
                res => {
                    resolve(res)
                        // console.log(res)
                    res_dispose(res)
                    clearTimeout(delay)
                    Indicator.close()


                }
            ).catch(
                err => {
                    clearTimeout(delay)
                    Indicator.close()
                    err_dispose(err) //错误处理
                    reject(err)

                }
            )

        })

    },
    post: function(url, info) {
        //post请求
        return new Promise(function(resolve, reject) {

            let delay = setTimeout(
                () => {
                    Indicator.open({
                        text: '加载中...',
                        spinnerType: 'fading-circle'
                    })
                },
                loadingdelay

            )

            //get请求
            axios.post(url, qs.stringify(info), { //qs序列化参数 做成stringformdate格式
                headers: { //头部配置
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                },
                // params: info,
                // paramsSerializer: function(params) {
                //     return Qs.stringify(params, { arrayFormat: 'brackets' })
                // },
            }).then(
                res => {

                    clearTimeout(delay)
                    Indicator.close()
                    res_dispose(res)
                    resolve(res) //解析结果为json
                }
            ).catch(
                err => {
                    clearTimeout(delay)
                    Indicator.close()
                    err_dispose(err)
                    reject(err)
                }
            )
        })

    },
    formAjax: function(url, formData) {
        return new Promise(function(resolve, reject) {

            let delay = setTimeout(
                () => {
                    Indicator.open({
                        text: '加载中...',
                        spinnerType: 'fading-circle'
                    })
                },
                loadingdelay

            )

            //get请求
            axios.post(url, formData, { //qs序列化参数 做成stringformdate格式
                headers: { //头部配置
                    'Content-Type': 'multipart/form-data'
                },

                // params: info,
                // paramsSerializer: function(params) {
                //     return Qs.stringify(params, { arrayFormat: 'brackets' })
                // },
            }).then(
                res => {

                    clearTimeout(delay)
                    Indicator.close()
                    resolve(res) //解析结果为json
                }
            ).catch(
                err => {
                    clearTimeout(delay)
                    Indicator.close()
                    err_dispose(err)
                    reject(err)
                }
            )
        })
    }
}